package com.example.androidapp2;

import java.util.List;
import com.example.androidapp2.DbClasses.ItemCategoria;
import com.xyzdesa.droidnomy.R;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class PopUpCategorias extends Dialog   {
	DbAdapter DbConect;
	Context Ctx;
	Boolean Agrega;	

	public EditText txtCate;
	public Button btnGuardar;	
	public Spinner spnCategorias;

	public PopUpCategorias(Context context, DbAdapter db) {
		super(context);
		Ctx = context;
		DbConect = db;
		this.setContentView(R.layout.pup_categorias);

		txtCate = (EditText) findViewById(R.id.txtCate);		
		btnGuardar = (Button) findViewById(R.id.btnGuardar);		
		spnCategorias = (Spinner) findViewById(R.id.spnCategorias);	

		SetListenerBotonesPopUpCategorias();
	}

	public void SetListenerBotonesPopUpCategorias()
	{	
		btnGuardar.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				if(Agrega){
					List<ItemCategoria> ls = DbConect.GetCategorias("spnCategorias",false,false);
					ItemCategoria it = null;
					for (ItemCategoria item : ls) {
						if(item.Descripcion.equals(txtCate.getText().toString()))
						{
							it = item;
							break;
						}
					}

					if(txtCate.getText().toString().length()>0){
						if(it == null){
							DbConect.InsertarCategoria(txtCate.getText().toString());
							((MainActivity)Ctx).CargarSpinnerCategorias();
							hide();
							txtCate.setText("");
						}
						else
							if(it.Estado==0){
								DbConect.ActivarCategoria(it.Codigo);
								((MainActivity)Ctx).CargarSpinnerCategorias();
								hide();			
								txtCate.setText("");
							}
					}
				}else
				{
					if(spnCategorias.getSelectedItem()!=null){
						((MainActivity)Ctx).pupConfirma.show("EliCategoria", new int[]{((ItemCategoria)spnCategorias.getSelectedItem()).Codigo});
						hide();					
					}
				}
			}
		});
	}

	public void CargarSpinnerCategorias()
	{
		ArrayAdapter<ItemCategoria> spinnerAdapter = new ArrayAdapter<ItemCategoria>(Ctx,android.R.layout.simple_spinner_item, DbConect.GetCategorias("spnCategorias",false,true));
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);		
		spnCategorias.setAdapter(spinnerAdapter);	
	}

	public void Show(Boolean agrega)
	{
		this.show();
		this.Agrega = agrega;	

		if(agrega){
			txtCate.setVisibility(View.VISIBLE);
			spnCategorias.setVisibility(View.INVISIBLE);
			this.setTitle(R.string.mnuAgCategorias);
			btnGuardar.setText(R.string.pupGuardar);
		}
		else
		{
			CargarSpinnerCategorias();
			txtCate.setVisibility(View.INVISIBLE);
			spnCategorias.setVisibility(View.VISIBLE);
			this.setTitle(R.string.mnuElCategorias);
			btnGuardar.setText(R.string.pupEliminar);
		}
	}


}
